package rsj.api.orgp;

import com.rlyy.aibase.utils.base.Box;
import com.rlyy.aibase.utils.base.MapNode;
import common.util.Assert;
import common.util.BoxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rsj.api.user.BaseLink;
import rsj.log.ModuleEnum;
import rsj.log.OperationLog;
import rsj.log.OperationTypeEnum;
import rsj.service.OrgProService;
import rsj.sql.user.UserDao;

@RestController
public class DoApplyProd extends BaseLink {

    private static final OrgProService orgProService = OrgProService.getInstance();

    @Autowired
    private UserDao userDao;

    /**
     * Org_Id
     * Prod_Id
     * Coll_Status
     * @param in
     * @return
     */
    @RequestMapping(value = "rsj_prod_apply")
    @OperationLog(description = "申请产品", logType = OperationTypeEnum.ADD, logModule = ModuleEnum.PRODUCT_MANAGEMENT)
    public Box executeLink(Box in){
        MapNode user = in.getMapNode("$user");
        String userId = user.getValue("User_Id");
        Assert.isNotBlank(userId, "用户id不可为空");
        MapNode mapNode = userDao.queryOrgIdByUser(userId);
        String userOrgId = mapNode.getValue("User_Org_Id");
        MapNode data = in.getMapNode("data");
        data.putNode("Org_Id",userOrgId);
        checkParams(data);
        String tid = in.getNode("$tid");
        String orgPId = orgProService.applyProd(data, tid);

        return BoxUtil.successBox(in, "申请成功！").putNode("Orgp_Id", orgPId);
    }

    private void checkParams(MapNode data) {
        Assert.isNotBlank(data.getValue("Prod_Id"), "产品ID不能为空！");
        Assert.isNotBlank(data.getValue("Org_Id"), "运营用户不能申请！");
    }


}
